'''
A cleaner version for gear class
'''

from math import sin, cos, tan, radians, degrees, floor, sqrt, acos, atan, pi,asin
from invfun import inv, inv_1
import os
import sys

'''
print重定向到txt或os.devnull
'''


class PrintToTxt:  # with HiddenPrints(self.hidelog):
    def __init__(self, filename='Default.txt', activated=True):
        # activated参数表示当前修饰类是否被激活
        self.filename = filename
        self.activated = activated
        self.original_stdout = None

    def change(self):
        sys.stdout.close()
        sys.stdout = self.original_stdout

    def closechange(self):
        self.original_stdout = sys.stdout
        sys.stdout = open(self.filename, 'w',
                          encoding=sys.getfilesystemencoding())

    def __enter__(self):
        if self.activated:
            self.closechange()

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.activated:
            self.change()


class HiddenPrints:  # with HiddenPrints(self.hidelog):
    def __init__(self, activated=True):
        # activated参数表示当前修饰类是否被激活
        self.activated = activated
        self.original_stdout = None

    def change(self):
        sys.stdout.close()
        sys.stdout = self.original_stdout

    def closechange(self):
        self.original_stdout = sys.stdout
        sys.stdout = open(os.devnull, 'w')
        # 这里的os.devnull实际上就是Linux系统中的“/dev/null”
        # /dev/null会使得发送到此目标的所有数据无效化，就像“被删除”一样
        # 这里使用/dev/null对sys.stdout输出流进行重定向

    def __enter__(self):
        if self.activated:
            self.closechange()

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.activated:
            self.change()


'''
单对齿轮副及其成员函数
'''


def gongfaxian(m, alpha, z, x,beta, K=None, returnK=False):
    alphat = atan(tan(alpha)/cos(beta))
    alphayt = acos(z*cos(alphat)/(z+2*x))
    betab = asin(sin(beta)*cos(alpha))
    mt = m/cos(beta)
    if K == None:
        #print(z/pi * (tan(alphayt)/cos(betab)**2-inv(alphat)-2*x*tan(alpha)/z)+0.5)
        #K = round(z/pi * (tan(alphayt)/cos(betab)**2-inv(alphat)-2*x*tan(alpha)/z)+0.5)
        pbn = pi*m*cos(alpha)
        Sbn = m*cos(alpha)*(pi/2+z*inv(alphat)+2*tan(alpha)*x)
        db = (mt*z)*cos(alphat)
        Wy = db*tan(alphayt)/cos(betab)
        K = (Wy-Sbn)/pbn + 1
        K=round(K)
    Wk = m*cos(alpha)*((K-0.5)*pi+z*inv(alphat))+2*x*m*sin(alpha)
    if returnK:
        return K, Wk
    else:
        return Wk


def liangzhu(m, alpha, z, x,beta, types='nei', Dm=None, returnKDm=False):
    alphat = atan(tan(alpha)/cos(beta))
    betab = asin(sin(beta)*cos(alpha))
    mt = m/cos(beta)
    q = 1 if types == 'wai' else -1
    d = mt*z
    if Dm == None:
        dy = d+2*m*x  # (11)
        alphayt = acos(d*cos(alphat)/dy)  # (9)
        st = m/cos(beta)*(pi/2+q*2*tan(alpha)*x)  # (13)
        syt = dy*(st/d+q*(inv(alphat)-inv(alphayt)))  # (14)
        nyt = (pi/z-syt/dy)  # (12) 弧度值
        Dm_float = dy*sin(nyt)/cos(alphayt+q*nyt)*cos(betab)  # 量柱球直径
        # 选取标准值的Dm
        Dmlist = [2, 2.25, 2.5, 2.75, 3, 3.25, 3.75, 4, 4.25, 4.5, 5, 5.25,
                  5.5, 6, 6.5, 7, 7.5, 8, 9, 10, 10.5, 11, 12, 14, 15,
                  16, 18, 20, 22, 25, 28, 30, 35, 40, 45, 50]
        if Dm_float<2:
            Dm=Dm_float
        else:
            for i in range(len(Dmlist)-1):
                if (Dmlist[i]<=Dm_float) and (Dm_float<Dmlist[i+1]):
                    Dm=Dmlist[i] if (Dm_float-Dmlist[i])<(Dmlist[i+1]-Dm_float) else Dmlist[i+1]
                    break
        
        

    K = (z-(z % 2))/2  # 跨越齿数

    #alphatm = inv_1(q*Dm/db+2*tan(alpha)*x/z-q*pi/2/z+inv(alpha))
    alphatm = inv_1(q*Dm/(m*z*cos(alpha))+2*tan(alpha)*x/z-q*pi/2/z+inv(alphat))
    if z % 2 == 0:
        Mk = m*z*cos(alphat)/cos(beta)/cos(alphatm)+q*Dm  # 量柱测量距
    else:
        Mk = m*z*cos(alphat)/cos(beta)/cos(alphatm)*cos(pi/2/z)+q*Dm  # 量柱测量距
    if returnKDm:
        return alphatm, Dm, K, Mk
    else:
        return Mk


class GearInit(object):  # 用于给齿轮类提供模块化的输入参数
    def __init__(self, b=(40, 40), ha_std=1, c_std=0.25, alpha_deg=20, xiubu=True, cuttype='gun', types='wai', hidelog=False, calcW=True):
        self.calcW = calcW

        self.cuttype = cuttype
        self.types = types
        self.hidelog = hidelog
        self.b = b  # 齿厚

        self.ha_std = ha_std
        self.c_std = c_std
        self.alpha = radians(alpha_deg)
        self.zmin = floor(2 * ha_std / sin(self.alpha)**2)
        self.xiubu = xiubu
        # 插齿刀参数 Default
        self.z0 = 58
        self.x0 = 0
        self.ha0_std = 1.25
        # 齿顶厚验算系数
        self.sa_std = 0.25
        # 行星轮齿顶圆修正方案1：大的齿顶圆直径舍弃
        self.revise = 1

        # 材料初始化
        self.material1 = '20CrMnTi'
        self.E1 = 207
        self.v1 = 0.25

        self.material2 = '20CrMnTi'
        self.E2 = 207
        self.v2 = 0.25
        # 齿轮精度初始化
        self.it1 = 6
        self.it2 = 6


class PlanetInit(object):
    def __init__(self, b=(40, 40, 40), it=(6, 6, 7), cuttype='gun', xiubu=True, hidelog=False, alpha_deg=20, ha_std=1, c_std=0.25, calcW=True,
                 K=[None, None, None], Dm=[None, None, None],
                 EbEy=['Eb', 'Eb', 'Ey']):
        self.Init_ac = GearInit(b=(b[0], b[1]),
                                cuttype='gun', types='wai', hidelog=hidelog, xiubu=xiubu, alpha_deg=alpha_deg, ha_std=ha_std, c_std=c_std, calcW=calcW)

        self.Init_cb = GearInit(b=(b[1], b[2]),
                                cuttype=cuttype, types='nei', hidelog=hidelog, xiubu=xiubu, alpha_deg=alpha_deg, ha_std=ha_std, c_std=c_std, calcW=calcW)
        self.K = K
        self.Dm = Dm
        self.EbEy = EbEy
        self.b = b  # 齿宽
        self.Init_ac.material1 = '20CrMnTi'
        self.Init_ac.E1 = 207
        self.Init_ac.v1 = 0.25

        self.Init_ac.material2 = '20CrMnTi'
        self.Init_ac.E2 = 207
        self.Init_ac.v2 = 0.25

        self.Init_cb.material1 = self.Init_ac.material2
        self.Init_cb.E1 = self.Init_ac.E2
        self.Init_cb.v1 = self.Init_ac.v2

        self.Init_cb.material2 = '40Cr'
        self.Init_cb.E2 = 211
        self.Init_cb.v2 = 0.277

        # 齿轮精度
        self.Init_ac.it1 = it[0]
        self.Init_ac.it2 = it[1]
        self.Init_cb.it1 = it[1]
        self.Init_cb.it2 = it[2]

        self.it1 = it[0]
        self.it3 = it[1]  # 行星轮
        self.it2 = it[2]  # 内齿


class Gear(object):
    def __init__(self, m, z1, z2, x1, x2, InitStruct: GearInit,beta_deg=0):
        self.beta = beta =radians(beta_deg) #螺旋线角度
        
        self.st_jump_max = 0  # 刚度跳变值
        self.b1 = InitStruct.b[0]
        self.b2 = InitStruct.b[1]
        self.InitStruct = InitStruct
        self.m, self.z1, self.z2, self.x1, self.x2 = m, z1, z2, x1, x2
        alpha, ha_std, c_std = InitStruct.alpha, InitStruct.ha_std, InitStruct.c_std
        self.alpha = alpha  # 齿形角（法相压力角、分度圆压力角）
        self.rou_f = c_std/(1-sin(alpha)) # 齿根圆角半径
        self.mt = mt = m/cos(self.beta) # 端面模数
        
        
        self.betab = asin(sin(beta)*cos(alpha))
        
        self.alphat = alphat = atan(tan(alpha)/cos(beta))  # 端面压力角
        
        self.d1 = d1 = mt*z1  # 分度圆直径
        self.d2 = d2 = mt*z2
        [self.db1, self.db2] = map(lambda x: cos(alphat)*x, [d1, d2])  # 基圆直径

        zmin = InitStruct.zmin  # 不根切最小齿数
        zmin=zmin*cos(beta)**3 
        x1min = ha_std*(zmin-z1)/zmin  # 不根切最小变位系数
        x2min = ha_std*(zmin-z2)/zmin
        self.BottomCut = [x1 >= x1min and x2 >=
                          x2min, x1 >= x1min, x2 >= x2min]  # 是否根切

        typeflag = 1 if InitStruct.types == 'wai' else -1
        self.x_segma = x_segma = x2 + typeflag*x1  # 变位系数和
        self.z_segma = z_segma = z2 + typeflag*z1  # 齿数和
        self.a = a = (d2 + typeflag*d1)/2  # 标准中心距

        self.alpha_work = self.alpha_workt = alpha_work = alpha_workt = inv_1(
            2*x_segma*tan(alpha)/z_segma + inv(alphat))  # 啮合角（节圆压力角）
        self.a_work = a_work = cos(alphat)*a/cos(alpha_workt)  # 实际中心距
        self.y = y = (a_work - a)/m  # 中心距变动系数（法向）
        self.yt = yt = (a_work - a)/mt  # 中心距变动系数（端面）
        
        self.delta_y = delta_y = x_segma - y  # 齿顶高变动系数（法向）
        self.s1 = pi*m/2 + 2*m*x1*tan(alpha)  # 分度圆齿厚（法向）
        self.s2 = pi*m/2 + typeflag*2*m*x2*tan(alpha)

        # 计算齿顶圆直径 并对于内齿轮直接根据过渡曲线不干涉要求修改齿顶圆直径
        types = InitStruct.types
        if InitStruct.cuttype == 'gun':  # 按滚刀计算内齿轮
            if types == 'wai':  # 外啮合计算
                self.da1 = d1 + 2*(ha_std + x1 - delta_y)*m  # 齿顶圆直径，下同
                self.df1 = d1 - 2*(ha_std + c_std - x1)*m  # 齿根圆直径，下同
                self.dNf1 = d1 - 2*(ha_std - x1)*m  # 渐开线起始圆直径
                self.da2 = d2 + 2*(ha_std + x2 - delta_y)*m
                self.df2 = d2 - 2*(ha_std + c_std - x2)*m
                self.dNf2 = d2 - 2*(ha_std - x2)*m
            else:
                self.da1 = d1 + 2*(ha_std + x1 + delta_y)*m
                self.df1 = d1 - 2*(ha_std + c_std - x1)*m
                self.dNf1 = d1 - 2*(ha_std - x1)*m
                self.da2 = d2 - 2*(ha_std - x2 + delta_y)*m
                self.df2 = d2 + 2*(ha_std + c_std + x2)*m
                self.dNf2 = d2 + 2*(ha_std + x2)*m

        else:  # 按插刀计算内齿轮
            if types == 'wai':
                self.da1 = d1 + 2*m*(ha_std + x1 - delta_y)
                self.da2 = d2 + 2*m*(ha_std + x2 - delta_y)
            else:
                hf1 = (ha_std + c_std - x1)*m  # 小齿轮齿根高
                self.df1 = df1 = d1 - 2*hf1  # 小齿轮齿根圆直径
                self.da2 = df1 + 2*a_work + 2*c_std*m

                da0 = m * (InitStruct.z0 + 2*InitStruct.ha0_std +
                           2*InitStruct.x0)  # 插齿刀齿顶圆直径
                alpha_work0 = inv_1(
                    inv(alpha) + 2*(x2-InitStruct.x0)*tan(alpha)/(z2-InitStruct.z0))  # 插内齿啮合角
                a_work0 = 0.5*m * (z2-InitStruct.z0) * \
                    cos(alpha) / cos(alpha_work0)  # 插内齿中心距

                self.df2 = df2 = da0 + 2*a_work0  # 大（内）齿轮齿根圆直径
                self.da1 = df2 - 2*a_work - 2*c_std*m

        if InitStruct.xiubu and types == 'nei':  # 内齿轮齿顶圆手动增大至不产生过渡曲线干涉
            rou = m*(0.5*z1*sin(alphat) - (ha_std-x1*cos(beta))/sin(alphat))
            self.da2_nei_min = sqrt(
                self.db2**2 + (2*a_work*sin(alpha_workt) + 2*rou)**2)  # 内啮合时，过渡曲线不干涉的最小内齿轮齿顶圆直径
            if self.da2 < self.da2_nei_min:
                # print(self.da2)
                self.da2 = self.da2_nei_min

    def calc(self):
        # 齿顶圆压力角
        self.alpha_a1 = alpha_a1 = acos(self.db1/self.da1)
        self.alpha_a2 = alpha_a2 = acos(self.db2/self.da2)

        # 最大滑动率
        u = self.z2/self.z1
        taw = tan(self.alpha_workt)  # 啮合角正切值
        ta1 = tan(alpha_a1)  # 齿顶圆压力角正切值
        ta2 = tan(alpha_a2)

        if self.InitStruct.types == 'wai':
            self.yeta1 = (1 + u) * (ta2 - taw)/((1 + u) * taw - u * ta2)
            self.yeta2 = (1 + u) * (ta1 - taw)/((1 + u) * taw - ta1)
        else:
            self.yeta1 = (1 - u) * (ta2 - taw)/((1 - u) * taw + u * ta2)
            self.yeta2 = (u - 1) * (ta1 - taw)/((u - 1) * taw + ta1)

        self.dyeta = abs(self.yeta1-self.yeta2)

        # 总重合度
        typeflag = 1 if self.InitStruct.types == 'wai' else -1
        self.ContactRatiot = 0.5/pi * \
            (self.z1*(ta1-taw) + typeflag*self.z2*(ta2-taw))
        self.ContactRatioBeta = min([self.b1,self.b2])*sin(self.beta)/pi/self.m
        self.ContactRatio = self.ContactRatiot + self.ContactRatioBeta
        # 过渡曲线干涉（如果xiubu==True，则不验证内齿轮）
        z1 = self.z1
        z2 = self.z2
        alpha = self.alpha
        ha_std = self.InitStruct.ha_std
        l1 = z2*ta2 - (z2 - z1)*taw
        r1 = z1*tan(alpha) - 4*(ha_std - self.x1)/sin(2*alpha)
        if self.InitStruct.types == 'wai':
            l2 = z1*ta1 - (z1 - z2)*taw
            r2 = z2*tan(alpha) - 4*(ha_std - self.x2)/sin(2*alpha)
            self.TransitionCurveInterference = [
                l1 >= r1 and l2 >= r2, l1 >= r1, l2 >= r2]
        else:
            if self.InitStruct.xiubu:
                self.TransitionCurveInterference = [True]*3
            else:
                self.TransitionCurveInterference = [l1 >= r1, l1 >= r1, True]

        # 齿廓重叠干涉
        if self.InitStruct.types == 'wai':
            self.ToothProfileInterference = [True]*3
        else:  # 'nei'
            ra1 = 0.5 * self.da1
            ra2 = 0.5 * self.da2
            aw = self.a_work
            delta1 = acos((ra2**2 - ra1**2 - aw**2) / (2*ra1*aw))
            delta2 = acos((ra2**2 - ra1**2 + aw**2) / (2*ra2*aw))
            l = self.z1*(delta1+inv(self.alpha_a1)) - self.z2*(delta2 +
                                                               inv(self.alpha_a2)) + inv(self.alpha_work)*(self.z2-self.z1)
            self.ToothProfileInterference = [l >= 0]*3

        # 齿顶厚验算
        def __sa(da, x, z, alpha_a):
            return da*((pi + 4*x*tan(alpha))/(2*z) + inv(alpha) - inv(alpha_a))
        r = self.InitStruct.sa_std * self.m
        sa1 = __sa(self.da1, self.x1, self.z1, self.alpha_a1)

        if self.InitStruct.types == 'wai':
            sa2 = __sa(self.da2, self.x2, self.z2, self.alpha_a2)
        else:  # 'nei'
            sa2 = r+1
        self.ToothTipThickness = [sa1 >= r and sa2 >= r, sa1 >= r, sa2 >= r]

        # 啮入重合度 啮出重合度
        self.ContactRatio1 = 0.5/pi * (self.z1*(ta1-taw))

        self.ContactRatio2 = 0.5/pi * (typeflag*self.z2*(ta2-taw))
        '''
        if self.InitStruct.calcW:
            # 公法线长度和量柱测量距（公称值）
            # 对小齿轮，都是外齿轮，采用公法线
            self.K1,self.Wk1 = gongfaxian(self.m, alpha, self.z1, self.x1,returnK=True)

            # 对大齿轮需要区分为外齿轮or内齿轮，内齿轮采用量柱测量距
            if self.InitStruct.types == 'wai':
                self.K2,self.Wk2 = gongfaxian(self.m, alpha, self.z2, self.x2,returnK=True)
            else:  # 'nei'
                self.Wk2 = -1
                self.alphatm2,self.Dm2,self.K2,self.Mk2 = liangzhu(self.m, alpha, self.z2, self.x2,returnKDm=True)
        '''
        self.EbEy = ['', '']
        self.E = [0, 0]
        self.K = [0, 0]
        self.Dm = [0, 0]
        self.alphatm = [0, 0]

    def PrintAllItem(self):  # 打印所有参数表
        print('\n'.join(['%s\t%s' % item for item in self.__dict__.items()]))


class PlanetaryGear(object):
    def __init__(self, m, za, zc, zb, xa, xc, xb, PlanetInitStruct: PlanetInit,beta_deg=0,calc=True):
        self.beta = radians(beta_deg)
        self.PlanetInitStruct = PlanetInitStruct
        self.alpha = alpha = self.PlanetInitStruct.Init_ac.alpha
        self.i = (1+zb/za)  # 传动比
        self.m = m
        self.za = za
        self.zc = zc
        self.zb = zb
        self.xa = xa
        self.xc = xc
        self.xb = xb
        self.gear_ac = Gear(m, za, zc, xa, xc, PlanetInitStruct.Init_ac,beta_deg)
        self.gear_cb = Gear(m, zc, zb, xc, xb, PlanetInitStruct.Init_cb,beta_deg)
        self.b1 = PlanetInitStruct.Init_ac.b[0]
        self.b3 = PlanetInitStruct.Init_ac.b[1]
        self.b2 = PlanetInitStruct.Init_cb.b[1]
        self.K = PlanetInitStruct.K
        self.Dm = PlanetInitStruct.Dm
        self.EbEy = PlanetInitStruct.EbEy

        if self.gear_cb.InitStruct.revise == 1:
            if self.gear_ac.da2 > self.gear_cb.da1:
                self.gear_ac.da2 = self.gear_cb.da1
            elif self.gear_ac.da2 < self.gear_cb.da1:
                self.gear_cb.da1 = self.gear_ac.da2
        self.gear_ac.calc()
        self.gear_cb.calc()

        self.ret_ac = self.gear_ac.BottomCut[0] and \
            self.gear_ac.ToothProfileInterference[0] and \
            self.gear_ac.TransitionCurveInterference[0] and \
            self.gear_ac.ToothTipThickness[0]

        self.ret_cb = self.gear_cb.BottomCut[0] and \
            self.gear_cb.ToothProfileInterference[0] and \
            self.gear_cb.TransitionCurveInterference[0] and \
            self.gear_cb.ToothTipThickness[0]

        self.E = [0, 0, 0]  # 存放公称值
        self.alphatm = [0, 0, 0]
        # 计算行星齿轮的公称公法线、量柱、以及用于换算到齿厚（圆周侧隙）的角度alphatm
        # 太阳
        if calc:
            if self.EbEy[0] == 'Eb':  # 公法线
                self.K[0], self.E[0] = gongfaxian(
                    m, alpha, za, xa,beta=self.beta, K=self.K[0], returnK=True)
            else:
                self.alphatm[0], self.Dm[0], _, self.E[0] = liangzhu(
                    m, alpha, za, xa,beta=self.beta, Dm=self.Dm[0], returnKDm=True, types='wai')
            if self.EbEy[1] == 'Eb':  # 公法线
                self.K[1], self.E[1] = gongfaxian(
                    m, alpha, zc, xc,beta=self.beta, K=self.K[1], returnK=True)
            else:
                self.alphatm[1], self.Dm[1], _, self.E[1] = liangzhu(
                    m, alpha, zc, xc,beta=self.beta, Dm=self.Dm[1], returnKDm=True, types='wai')
            if self.EbEy[2] == 'Eb':  # 公法线
                self.K[2], self.E[2] = gongfaxian(
                    m, alpha, zb, xb,beta=self.beta, K=self.K[2], returnK=True)
            else:
                self.alphatm[2], self.Dm[2], _, self.E[2] = liangzhu(
                    m, alpha, zb, xb,beta=self.beta, Dm=self.Dm[2], returnKDm=True, types='nei')

            # 输入进各齿轮副类
            self.gear_ac.K = self.K[0:2]
            self.gear_cb.K = self.K[1:3]
            self.gear_ac.Dm = self.Dm[0:2]
            self.gear_cb.Dm = self.Dm[1:3]
            self.gear_ac.E = self.E[0:2]
            self.gear_cb.E = self.E[1:3]
            self.gear_ac.EbEy = self.EbEy[0:2]
            self.gear_cb.EbEy = self.EbEy[1:3]
            self.gear_ac.alphatm = self.alphatm[0:2]
            self.gear_cb.alphatm = self.alphatm[1:3]

    def PrintItem(self):
        print('-------------')
        print('z1=%d,z3=%d,z2=%d\t' % (self.za, self.zc, self.zb), end='')
        print('x1=%f,x3=%f,x2=%f\t' % (self.xa, self.xc, self.xb), end='')
        print('da1=%f,da3=%f,da2=%f' %
              (self.gear_ac.da1, self.gear_ac.da2, self.gear_cb.da2))
        print('Pair ac:yeta1=%f,yeta3=%f,\tdyeta13=%f,\t' % (
            self.gear_ac.yeta1, self.gear_ac.yeta2, self.gear_ac.dyeta), end='')
        print('ContactRatio=%f,\talpha_work=%f' %
              (self.gear_ac.ContactRatio, degrees(self.gear_ac.alpha_work)))
        print('Pair cb:yeta3=%f,yeta2=%f,\tdyeta32=%f,\t' % (
            self.gear_cb.yeta1, self.gear_cb.yeta2, self.gear_cb.dyeta), end='')
        print('ContactRatio=%f,\talpha_work=%F' %
              (self.gear_cb.ContactRatio, degrees(self.gear_cb.alpha_work)))
        # print('-------------')

    def MiniPrint(self):
        print('x1=%f,x3=%f,x2=%f\t' % (self.xa, self.xc, self.xb), end='')
        print('dyeta13=%f,dyeta32=%f,\t' %
              (self.gear_ac.dyeta, self.gear_cb.dyeta), end='')
        print('CRatio=%f,%f\t' %
              (self.gear_ac.ContactRatio, self.gear_cb.ContactRatio), end='')
        print('a_work=%f,%f\t' % (degrees(self.gear_ac.alpha_work),
                                  degrees(self.gear_cb.alpha_work)), end='')
        if self.gear_ac.st_jump_max != 0:
            print('st_jump=%f,%f' %
                  (self.gear_ac.st_jump_max, self.gear_cb.st_jump_max))
        else:
            print('stiffjump(=0) may be not calced')


if __name__ == '__main__':
    '''
    InitStruct = InitClass(xiubu=True, types='wai', cuttype='gun')
    gear1 = Gear(1.75, 18, 44, 0.5863, 0.691, InitStruct)
    gear1.calc()
    #gear1.PrintAllItem()
    #InitStruct.revise=0
    '''

    #PtInit = PlanetInit(cuttype='gun', xiubu=True)
    #pt1 = PlanetaryGear(1.75, 18, 45, 108, 0.393, 0.0285, 0.45, PtInit)

    # pt1.gear_cb.PrintAllItem()
    # pt1.gear_ac.PrintAllItem()

    # print(gongfaxian(pt1.gear_ac.m,pt1.gear_ac.alpha,pt1.gear_ac.z2,pt1.gear_ac.x2))
    #_,Dm,_,Ey=liangzhu(1.75, radians(20), 108, 0.45, types='nei', Dm=None,returnKDm=True)
    #print(Dm,Ey)
    
    #K, E = gongfaxian(
    #            1.75, radians(20), 18, 0.393, K=None, returnK=True)
    #print(K,E)
    # print(pt1.gear_cb.Mk2)
    
    '''
    print('xie----')
    print(gongfaxian(1.75,radians(20),18,0.393,radians(20),K=3,returnK=True))
    print(gongfaxian(1.75,radians(20),45,0.0285,radians(20),returnK=True))
    print(liangzhu(1.75,radians(20),45,0.0285,radians(20),types='wai',Dm=None,returnKDm=True))
    print(liangzhu(1.75,radians(20),18,0.393,radians(20),types='wai',Dm=3.5,returnKDm=True))
    '''
    
    init = PlanetInit()
    pt = PlanetaryGear(2,27,41,108,0.14,-0.11,0.45,init)
    
    print(pt.gear_ac.dyeta)
    print(pt.gear_cb.dyeta)